<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){

        $category = D('Category')->getTree();
        $lists    = D('Document')->lists(null);

        $this->assign('category',$category);//栏目
        $this->assign('lists',$lists);//列表
        $this->assign('page',D('Document')->page);//分页

                 
        $this->display();
    }
    /*
     * 登录页面
     *
     * @params varchar mobile     电话号码
     * @params varchar pwd   验证码
     * */
    public function login(){

        $resu = array('code' => 1,'msg' => '登陆成功','data' => array());
        $mob = array('mobile' => $_POST['mobile']);
        $note = md5(json_encode($_POST['password']));
        $db = M('personal');//实例化personal表

        $token = array(//接受电话号码并且创建token登陆时间和退出时间
            'mobile' => $_POST['mobile'],
            'ctime' => time(),
            'out_time' => (time()+36000*24*7)
        );
        $toke = base64_encode(json_encode($token));//加密token
        $data['last_login_time'] = $token['ctime'];
        $data['token'] = $toke;
        $yanzhengma = $db->where($mob)->field('password')->find();
        $yanzheng = implode($yanzhengma);
        $list = $db->where($mob)->find();
        if ($list){
            if($yanzheng == $note){//如果密码匹配那就登陆成功
                $db->where($mob)->save($data);
                $resu['data'] = $toke;
                $db->where($mob)->save(array('authcode' => '----------'));//清空验证码
            }else{
                $resu['code'] = 2;
                $resu['msg'] = '登录失败,验证码或手机号错误!';
            }
        }else{
            $resu['code'] = 2;
            $resu['msg'] = '请先注册!';
        }
        $this -> ajaxReturn($resu);
    }
    /*
     * 忘记密码发送短信接口
     *
     * */
    public function authcode(){

        $resu = array('code' => 1,'msg' => '短信发送成功!','data' => array());
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $yanzheng = rand(100000,999999);//定义一个随机六位数的变量
        $smsConf = array(
            'key'   => '5ff30d5a8e99e043e3a245d69810b7dd', //您申请的APPKEY
            'mobile'    => $_POST['mobile'], //接受短信的用户手机号码
            'tpl_id'    => '98177', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#='.$yanzheng //您设置的模板变量，根据实际情况修改
        );
        $dt = M('personal');
        $data = array('authcode' => $yanzheng);
        $where = array('mobile' => $_POST['mobile']);
        $code = $dt->where($where)->find();
        if($code){
            $dt->where($where)->save($data);
            $_SESSION[$smsConf['mobile']] = $yanzheng;//将验证码绑定在发送验证码的手机上
            $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
            if($content){
                $result = json_decode($content,true);
                $error_code = $result['error_code'];
                if($error_code == 0){//状态为0，说明短信发送成功
                    $resu['msg'] = '短信发送成功!请注意查收';
                }else{//状态非0，说明失败
                    $msg = $result['reason'];
                    $resu['code'] = 2;
                    $resu['msg'] = "短信发送失败!";
                }
            }else {
                $resu['code'] = 2;
                $resu['msg'] = "短信发送失败!";
            }
        }else{
            $resu['code'] = 2;
            $resu['msg'] = "您还没有注册!";
        }
        $this -> ajaxReturn($resu);
    }
    /*
     * 忘记密码
     * */
    public function forget(){
        $resu = array('code' => 1,'msg' => '密码修改成功!','data' => array());
        $mobile = $_POST['mobile'];
        $authcode = $_POST['authcode'];
        $newpwd = md5(json_encode($_POST['password']));
        $array = array('password' => $newpwd,'authcode'=>'----------');
        $where = array('mobile' => $mobile);
        $data = M('personal')->where($where)->find();
        if($data){
            $auth = implode(M('personal')->where($where)->field('authcode')->order('id desc')->find());
            if($auth){
                if($auth == $authcode){
                    M('personal')->where($where)->save($array);
                }else{
                    $resu['code'] = 2;
                    $resu['msg'] = "验证码输入错误!";
                }
            }else{
                $resu['code'] = 2;
                $resu['msg'] = "请先获取验证码!";
            }
        }else{
            $resu['code'] = 2;
            $resu['msg'] = "您还没有注册!";
        }
        $this->ajaxReturn($resu);
    }
    /*
     * 个人信息
     * @ HTTP_TOKEN 缓存中的token,用于调取手机号码
     * @ persnal 个人信息表
     * @ resu 返还数据
     * */
    public function information(){
        $resu = array('code' => 1,'msg' => '个人信息查询成功!','data' => array());
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $data = M('personal')->where(array('mobile' => $mobile))->field('username,mobile,balance,integral')->find();
        if($data){
            $resu['data'] = $data;
        }else{
            $resu['code'] = 2;
            $resu['msg'] = "个人信息查询失败!";
        }
        $this->ajaxReturn($resu);
    }
    /*
     * 修改昵称
     * @ HTTP_TOKEN 缓存中的token,用于调取手机号码
     * @ params varchar newname 新的昵称
     * @ persnal 个人信息表
     * @ resu 返还数据
     * */
    public function sett(){
        $resu = array('code' => 1,'msg' => '昵称修改成功!','data' => array());
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $array = array('username' => $_POST['newname']);
        $save = M('personal')->where(array('mobile' => $mobile))->save($array);
        if($save){

        }else{
            $resu['code'] = 2;
            $resu['msg'] = "昵称修改失败!";
        }
        $this->ajaxReturn($resu);
    }
    /*
     * 注册发送短信接口
     *
     * */
    public function authcode_register(){

        $resu = array('code' => 1,'msg' => '短信发送成功!','data' => array());
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $yanzheng = rand(100000,999999);//定义一个随机六位数的变量
        $smsConf = array(
            'key'   => '5ff30d5a8e99e043e3a245d69810b7dd', //您申请的APPKEY
            'mobile'    => $_POST['mobile'], //接受短信的用户手机号码
            'tpl_id'    => '98177', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#='.$yanzheng //您设置的模板变量，根据实际情况修改
        );
        if($_POST['mobile']){
            $array = array('mobile' => $_POST['mobile'],'authcode' => $yanzheng);
            M('authcode')->add($array);
            $_SESSION[$smsConf['mobile']] = $yanzheng;//将验证码绑定在发送验证码的手机上
            $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
            if($content){
                $result = json_decode($content,true);
                $error_code = $result['error_code'];
                if($error_code == 0){//状态为0，说明短信发送成功
                    //$resu['data'] = $token_register;
                    $resu['msg'] = '短信发送成功!请注意查收';
                }else{//状态非0，说明失败
                    $msg = $result['reason'];
                    $resu['code'] = 2;
                    $resu['msg'] = "短信发送失败!";
                }
            }else {
                $resu['code'] = 2;
                $resu['msg'] = "短信发送失败!";
            }
        }else{
            $resu['code'] = 2;
            $resu['msg'] = "请输入正确的电话号码!";
        }
        $this -> ajaxReturn($resu);
    }
    /*
     * 注册
     * @ HTTP_TOKEN_REGISTER 缓存中的token,用于调取验证码
     * @ personal 个人信息表
     * @ params int authcode 输入的验证码
     * @ params int mobile 输入的电话号码
     * @ params int username 输入的密码
     * @ return resu 返还数据
     * */
    public function register (){
        $resu = array('code' => 1,'msg' => '注册成功!');
        $mobile = $_POST['mobile'];
        $list = M('authcode')->where(array('mobile'=>$mobile))->field('authcode')->order('id desc')->find();
        $authcode = $_POST['authcode'];
        $username = $_POST['username'];
        $password = md5(json_encode($_POST['password']));
        $data = M('personal')->where(array('mobile' => $mobile))->find();
        $superior = base64_decode($_POST['superior']);
        $superior_cx = M('personal')->where(array('mobile' => $superior))->find();
        if($superior_cx){
            $array = array('mobile' => $mobile , 'username' => $username , 'password' => $password,'registration_time' => time(),'superior' => $superior);
            if($superior){
                if($data){
                    $resu['msg'] = "您已经注册过,请直接登录!";
                }else{
                    if($mobile != NULL && $username != NULL && $password != NULL && $authcode != NULL){
                        if($list&&$list['authcode'] == $authcode){
                            M('authcode')->where(array('mobile' => $mobile))->delete();
                            $add = M('personal')->add($array);
                            if($add){

                            }else{
                                $resu['code'] = 2;
                                $resu['msg'] = "注册失败,请重试!";
                            }
                        }else{
                            $resu['code'] = 2;
                            $resu['msg'] = "验证码输入错误!";
                        }
                    }else{
                        $resu['code'] = 2;
                        $resu['msg'] = "请输入完整信息!";
                    }
                }
            }else{
                $resu['code'] = 2;
                $resu['msg'] = "还没有人邀请您注册哦!";
            }
        }else{
            $resu['code'] = 2;
            $resu['msg'] = "该邀请链接无效!";
        }
        $this->ajaxReturn($resu);
    }
    /*
     * 退出登录
     * @ personal 个人信息表
     * @ resu 返还数据
     * */
    public function loginout(){
        $resu = array('code' => 1,'msg' => '退出成功!');
        $tokk = $_SERVER['HTTP_TOKEN'];
        $toke = json_decode(base64_decode($tokk),true);//解密token
        $mobile = $toke['mobile'];//赋值
        $db = M('personal')->where(array('mobile' => $mobile))->save(array('token' => ''));
        if($db){

        }else{
            $resu['code'] = 2;
            $resu['msg'] = '退出失败!';
        }
        $this->ajaxReturn($resu);
    }
}